package com.squid.cloud.common.dto;

import com.alibaba.fastjson.JSON;
import com.squid.cloud.common.enums.ErrorCodeEnum;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@ApiModel(value = "通用返回对象", description = "微服务之间，或者内部接口通用返回对象")
public class BaseResponseDto<T> {

	@ApiModelProperty(value = "调用结果描述", name = "msg", example = "操作成功!")
	String msg;
	@ApiModelProperty(value = "错误码", name = "code", example = "100")
	String code;
	@ApiModelProperty(value = "存储扩展数据", name = "data", example = "")
	T data;

	public BaseResponseDto(String msg, String msgcode, T data) {
		this.msg = msg;
		this.code = msgcode;
		this.data = data;
	}

	public BaseResponseDto(ErrorCodeEnum code, T data) {
		this.msg = code.getDesc();
		this.code = code.getCode();
		this.data = data;
	}

	public BaseResponseDto() {
	}

	public void fail(String msg, String msgcode) {
		this.msg = msg;
		this.code = msgcode;
	}

	public void fail(String msg) {
		this.code = ErrorCodeEnum.FAIL.getCode();
		this.msg = msg;
	}
	
	public void fail(ErrorCodeEnum code){
		this.msg = code.getDesc();
		this.code = code.getCode();
	}

	public void fail() {
		this.code = ErrorCodeEnum.FAIL.getCode();
		this.msg = ErrorCodeEnum.FAIL.getDesc();
	}

	public void success() {
		this.msg = ErrorCodeEnum.SUCCESS.getCode();
		this.code = ErrorCodeEnum.SUCCESS.getDesc();
	}
	
	public void other(ErrorCodeEnum code){
		this.msg = code.getDesc();
		this.code = code.getCode();
	}
	
	public void success(T data) {
		this.msg = ErrorCodeEnum.SUCCESS.getCode();
		this.code = ErrorCodeEnum.SUCCESS.getDesc();
		this.data = data;
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}
}
